<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link rel="stylesheet" href="../element-ui/element.css">
    <style>
        .register {
            width: 100%;
            height: 100%;
            background-size: 100% 100%;
         }
        .demo-ruleForm{
            width: 400px;
            height: 400px;
        }
    </style>
</head>
<body>
<script src="../../vue/vue.js"></script>
<script src="../element-ui/element.js"></script>
<script src="../../jquery/jquery-3.4.1.js"></script>
<div id="register">
    <template>
        <div class="register">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username" ref="name">
                    <el-input v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="ruleForm.phone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="success" @click="submitForm('ruleForm')">注册</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </template>
</div>
</body>
<script>
    var Main = {
        name: 'register',
        data() {

            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'))
                } else if (value !== this.ruleForm.password) {
                    callback(new Error('两次输入密码不一致!'))
                } else {
                    callback()
                }
            };
            return {
                activeName: 'second',
                ruleForm: {
                    username: '',
                    password: '',
                    checkPass: '',
                    phone: ''
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 2, max: 5, message: '长度在2-5个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '密码不能为空', trigger: 'blur'},
                        {min: 6, max: 16, message: '密码必须是6-16位', trigger: 'blur'},
                        {
                            pattern: /^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S/,
                            message: '密码必须包含数字、字母和下划线任意两种',
                            trigger: 'blur'
                        }
                    ],
                    checkPass: [
                        {required: true, message: '确认密码不能为空', trigger: 'blur'},
                        {validator: validatePass2, trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                        // 这个只能验证手机号
                        {
                            pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|17[1-9]|18[1-9])[0-9]{8}$/,
                            message: '请输入合法手机号',
                            trigger: 'blur'
                        }
                    ]
                }
            }
        },
        methods: {
            submitForm(formName) {
              //通过ref定位到form表单
              this.$refs[formName].validate(valid => {
                if (valid) {
                    $.ajax({
                        type: 'post',
                        url: '/forum/user/register',
                        dataType: 'json',
                        data:{
                            userName: this.ruleForm.username,
                            userPassword: this.ruleForm.password,
                            userPhone:this.ruleForm.phone,
                        },
                        success: function (data) {
                            console.log(data)
                            if (data.state === '200') {
                                alert("注册成功");
                            }
                        }
                    })
                }
              });
            },
            resetForm(formName) {
                // 实现重置表单元素数据
                this.$refs[formName].resetFields()
            }
        }
    }
    var Ctor = Vue.extend(Main);
    new Ctor().$mount('#register');
</script>